import {
  _cities,
  _departments,
  _list
} from '../../common/api/test'
import {
  formatDate
} from '../../utils/util'
const app = getApp()
Page({
  data: {
    type: '',
    cities: [],
    cityIndex: null,
    departments: [],
    departmentIndex: null,
    name: '',
    list: [],
    finished: false,
    totalCount: null,
    pageIndex: 1,
    pageSize: 10
  },
  inputHandler(e) {
    this.data.name = e.detail
  },
  cityChange(e) {
    this.setData({
      cityIndex: e.detail.value
    }, () => {
      this.search()
    })
  },
  departmentChange(e) {
    this.setData({
      departmentIndex: e.detail.value
    }, () => {
      this.search()
    })
  },
  // 进页面合并查询
  totalQuery() {
    app.loading('加载中')
    let memberid = wx.getStorageSync('memberid')
    let city = this.data.cityIndex !== null ? this.data.cities[this.data.cityIndex] : ''
    let department = this.data.departmentIndex !== null ? this.data.departments[this.data.departmentIndex] : ''
    let name = this.data.name
    let exttype = this.data.type
    Promise.all([
      _list( //查询列表
        memberid,
        city,
        department,
        name,
        exttype,
        this.data.pageIndex,
        this.data.pageSize
      )
    ]).then(res => {
      wx.hideLoading()
      // let cities = res[0].data.Data
      // let departments = res[1].data.Data
      let totalCount = res[0].data.Data.Count
      let list = res[0].data.Data.Data.map(item => {
        item.Statime = formatDate(new Date(item.Statime), 'yyyy-MM-dd')
        item.EndTime = formatDate(new Date(item.EndTime), 'yyyy-MM-dd')
        return item
      })
      let finished = list.length >= totalCount
      this.setData({
        list,
        finished,
        totalCount
      })
    }).catch(err => {
      wx.hideLoading()
      console.log(err)
      wx.showModal({
        title: '对不起',
        content: '请求错误，请稍后再试！',
        showCancel: false
      })
    })
  },
  search() {
    this.setData({
      pageIndex: 1
    })
    let memberid = wx.getStorageSync('memberid')
    let city = this.data.cityIndex !== null ? this.data.cities[this.data.cityIndex] : ''
    city = ''
    let department = this.data.departmentIndex !== null ? this.data.departments[this.data.departmentIndex] : ''
    department = ''
    let name = this.data.name
    let exttype = this.data.type
    app.loading('加载中')
    _list( //查询列表
      memberid,
      city,
      department,
      name,
      exttype,
      this.data.pageIndex,
      this.data.pageSize
    ).then(res => {
      wx.hideLoading()
      let totalCount = res.data.Data.Count
      let list = res.data.Data.Data.map(item => {
        item.Statime = formatDate(new Date(item.Statime), 'yyyy-MM-dd')
        item.EndTime = formatDate(new Date(item.EndTime), 'yyyy-MM-dd')
        return item
      })
      let finished = list.length >= totalCount
      this.setData({
        list,
        finished
      })
    }).catch(err => {
      wx.hideLoading()
      console.log(err)
      wx.showModal({
        title: '对不起',
        content: '请求错误，请稍后再试！',
        showCancel: false
      })
    })
  },
  concatList() {
    let memberid = wx.getStorageSync('memberid')
    let city = this.data.cityIndex !== null ? this.data.cities[this.data.cityIndex] : ''
    city = ''
    let department = this.data.departmentIndex !== null ? this.data.departments[this.data.departmentIndex] : ''
    department = ''
    let name = this.data.name
    let exttype = this.data.type
    _list(
      memberid,
      city,
      department,
      name,
      exttype,
      this.data.pageIndex,
      this.data.pageSize
    ).then(res => {
      let list = res.data.Data.Data.map(item => {
        item.Statime = formatDate(new Date(item.Statime), 'yyyy-MM-dd')
        item.EndTime = formatDate(new Date(item.EndTime), 'yyyy-MM-dd')
        return item
      })
      this.data.list = this.data.list.concat(list)
      this.setData({
        list: this.data.list
      })
    }).catch(err => {
      console.log(err)
      wx.showModal({
        title: '对不起',
        content: err || '网络错误，请稍后再试',
        showCancel: false
      })
    })
  },
  onReachLower() {
    if (this.data.finished) {
      return
    }
    let list = this.data.list
    let totalCount = this.data.totalCount
    if (list.length >= totalCount) {
      this.data.finished = true
      this.setData({
        finished: this.data.finished
      })
    } else {
      this.data.pageIndex += 1
      console.log(1)
      this.concatList()
    }
  },
  onLoad(options) {
    this.data.type = options.type || this.data.type
    this.setData({
      type: this.data.type
    })
    wx.setNavigationBarTitle({
      title: this.data.type
    })
  },
  onReady() {},
  onShow() {
    if (app.store.userinfo) {
      this.data.userinfo = app.store.userinfo
      this.totalQuery()
    } else {
      app.loading('加载中')
      app.launch().then(res => {
        wx.hideLoading()
        console.log(res)
        this.data.userinfo = res
        this.totalQuery()
      }).catch(err => {
        wx.hideLoading()
        console.log(err)
      })
    }
  },
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}
})